<?php
class Controller_Financial extends  Controller_Template 
{
    public function __construct($req, $res) 
    {
		$this->template = "smarty:estimate/financial/current";
		parent::__construct($req, $res);
	}

    public function action_index ()
    {
        $this->action_current();
    }

    /**
     * 当前未完成的结算信息
     */
    public function action_current ()
    {
        $channel_name = $_GET['channel_name'];
        $this->template->set('lists', estimate::lists(user::current_user_id(), $channel_name));
        $this->template->set_filename('smarty:estimate/list');
        $this->template->set('channel_name', $channel_name);
        return;

    }

    public function action_detail ($id)
    {
        $channel_name = $_GET['channel_name'];
        $this->template->set('lists', estimate::detail(user::current_user_id(), $id, $channel_name));
        $this->template->set_filename('smarty:estimate/detail');
        $this->template->set('channel_name', $channel_name);
        $this->template->set('id', $id);
        return;
    }

    public function action_apply ($id)
    {
        estimate::set_status(user::current_user_id(), $id, 1);
        $this->request->redirect('/financial/current');
    }

    public function action_agree ($id)
    {
        estimate::set_status(user::current_user_id(), $id, 2);
        $this->request->redirect('/financial/current');
    }

    public function action_paid ($id)
    {
        estimate::set_status(user::current_user_id(), $id, 3);
        $this->request->redirect('/financial/current');
    }



    public function action_bill ($id)
    {
        $id   = (int) $id;
        $this->template->set_filename('smarty:estimate/financial/bill');
        $bill = Estimate_Financial::bill($id);
        $this->template->set('bill', $bill);
    }

    /**
     * 当前已完成的结算信息
     */
    public function action_finished ()
    {
        $query  = array(
            'status' => 'merchant_confirmed',
            'include_merchant_info' => TRUE,
            'include_activity_info' => TRUE,
            'include_vendor_info'   => TRUE,
        );

        $result = Estimate_Financial::search($query, $offset = NULL, $limit = NULL);
        $this->template->set_filename('smarty:estimate/financial/history');
        $this->template->set('financials', $result);
    }


    /**
     * 处理结算动作
     *
     */
    public function action_verify ($financial_id, $status)
    {
        $financial = new Estimate_Financial($financial_id);
        $financial->set_status($status);
        $this->request->redirect('/financial');
    }
}
